package com.zrf.dao.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.zrf.pojo.Check;
import com.zrf.pojo.Manifest;
import com.zrf.pojo.Product;
import com.zrf.pojo.Storage;
import com.zrf.pojo.User;

/**仓库变动信息*/
@Repository("manifestDao")
public class ManifestDaoImpl extends BaseDaoImpl<Manifest> {
	private Session session;
	private Transaction tran;
	private String hql = "";
	private List<?> list = null;
	private Timestamp time = null;
	
	@SuppressWarnings("deprecation")
	public void addLogo(Storage storage, Storage toStorage, Product products, Integer pnum, User user) {
		// TODO Auto-generated method stub

		Check c = null;
		Check c2 = null;
		Manifest l = null;
		session = this.getHibernateTemplate().getSessionFactory().openSession();
		tran = session.beginTransaction();
		
		time = new Timestamp((new Date()).getTime());
		hql = "FROM Check c WHERE c.storage.id=? AND c.products.storage.id=?";
		list = session.createQuery(hql).setInteger(0, storage.getSid()).setInteger(1, storage.getSid()).list();
		if (list==null||list.size()<1) {
			c = new Check(storage, products, pnum, time.getYear(), time.getMonth());
		}else {
			c = (Check) list.get(0);
		}
		
		l = new Manifest(user, toStorage, products, pnum, products.getPprice(), 1, time);
		session.save(l);
		l = new Manifest(user, storage, products, pnum, products.getPprice(), 0, time);
		session.save(l);
		
		products.setPnum(products.getPnum()-pnum);
		c.setNum(products.getPnum());
		
		String hsql = "FROM Products p WHERE p.name=? AND p.storage.id=?";
		List<?> list2 = session.createQuery(hsql).setString(0, products.getPname()).setInteger(1, toStorage.getSid()).list();
		Product product = null;
		if (list2==null||list2.size()<1) {
			product = new Product(toStorage, products.getPname(), pnum, products.getPprice());
		}else {
			product = (Product) list2.get(0);
			product.setPnum(product.getPnum()+pnum);
		}
		list = session.createQuery(hql).setInteger(0, toStorage.getSid()).setInteger(1, toStorage.getSid()).list();
		if (list==null||list.size()<1) {
			
			c2 = new Check(toStorage, product, pnum, time.getYear(), time.getMonth());
		}else {
			c2 = (Check) list.get(0);
			c2.setNum(product.getPnum());
		}
		session.update(products);
		session.saveOrUpdate(c);
		session.saveOrUpdate(product);
		session.saveOrUpdate(c2);
		
		tran.commit();
		session.close();
	}


	@SuppressWarnings("deprecation")
	public void addLogo(Storage storage, String address, Product product, int pnum,
			User user) {
		// TODO Auto-generated method stub

		Check c = null;
		Manifest l = null;
		session = this.getHibernateTemplate().getSessionFactory().openSession();
		tran = session.beginTransaction();
		
		time = new Timestamp((new Date()).getTime());
		hql = "FROM Check c WHERE c.storage.id=? AND c.products.storage.id=?";
		list = session.createQuery(hql).setInteger(0, storage.getSid()).setInteger(1, storage.getSid()).list();
		if (list==null||list.size()<1) {
			c = new Check(storage, product, pnum, time.getYear(), time.getMonth());
		}else {
			c = (Check) list.get(0);
		}
		
		l = new Manifest(user, product, pnum, product.getPprice(), 1, time, address);
		
		product.setPnum(product.getPnum()-pnum);
		c.setNum(product.getPnum());
		session.save(l);
		session.update(product);
		session.saveOrUpdate(c);
		
		
		tran.commit();
		session.close();
		
	}

}